package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.simpleframework.xml.core.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;





	@TsLogin
	@Operation(summary ="批量获取下单付费声音列表")
	@GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId,@PathVariable("trackCount") Integer trackCount){
		Long userId = AuthContextHolder.getUserId();
		List<TrackInfo> list= trackInfoService.findPaidTrackInfoList(trackId,trackCount,userId);

		return Result.ok(list);
	}



	@TsLogin
	@Operation(summary ="声音分集列表")
	@GetMapping("/findUserTrackPaidList/{trackId}")
	public Result findUserTrackPaidList(@PathVariable Long trackId){

		Long userId = AuthContextHolder.getUserId();

		List<Map<String,Object>> list =  trackInfoService.findUserTrackPaidList(trackId,userId);


		return Result.ok(list);
	}


	@TsLogin(required = false)
	@Operation(summary ="根据专辑Id获取声音列表")
	@GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
	public Result findAlbumTrackPage(@PathVariable Long albumId,
									 @PathVariable Long pageNo,
									 @PathVariable Long pageSize){
		Long userId = AuthContextHolder.getUserId();
		Page<AlbumTrackListVo> trackListVoPage = new Page<>(pageNo, pageSize);
		IPage<AlbumTrackListVo> iPage = trackInfoService.findAlbumTrackPage(trackListVoPage,albumId,userId);
		return Result.ok(iPage);
	}

	@Operation(summary="修改声音")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable Long trackId, @RequestBody TrackInfoVo trackInfoVo){
		//调用服务层方法
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		//  返回数据
		return Result.ok();
	}

	@Operation(summary="根据声音Id回显声音对象")
	@GetMapping("/getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable Long trackId){
		//  调用服务层方法 trackInfoService.getTrackInfo(trackId);
		TrackInfo trackInfo=this.trackInfoService.getById(trackId);
		//  返回数据
		return Result.ok(trackInfo);
	}

	@Operation(summary = "根据声音Id删除声音对象")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable Long trackId){
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	@Operation(summary = "根据用户Id，状态等条件查询声音列表")
	@PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
	public Result findUserTrackPage(@PathVariable long pageNo,
	@PathVariable long pageSize, @RequestBody TrackInfoQuery trackInfoQuery){
		//获取用户Id
		long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		//构建分页对象
		Page<TrackListVo> trackListVoPage = new Page<>(pageNo, pageSize);
		//调用服务层方法
		IPage<TrackListVo> iPage =this.trackInfoService.findUserTrackPage(trackListVoPage,trackInfoQuery);
		//返回数据
		return Result.ok(iPage);

	}

	@Operation(summary = "保存声音")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validate TrackInfoVo trackInfoVo){
		//获取用户id
		long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		//调用服务层方法
		trackInfoService.saveTrackInfo(trackInfoVo, userId);
		//返回数据
		return Result.ok();
	}


	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file){
		//	调用服务层方法：将声音存储到什么位置? vod 云点播
		Map<String,Object> map = trackInfoService.uploadTrack(file);
		//	返回数据
		return Result.ok(map);
	}



}

